package com.angcyo.javafx

import com.angcyo.http.rx.mainScheduler
import com.angcyo.javafx.base.ex.getImageFx
import com.angcyo.log.L
import io.reactivex.rxjavafx.schedulers.JavaFxScheduler
import javafx.application.Application
import javafx.application.Platform
import javafx.scene.image.Image
import javafx.stage.Screen
import javafx.stage.Stage
import java.nio.charset.Charset
import java.security.cert.X509Certificate
import javax.net.ssl.HttpsURLConnection
import javax.net.ssl.SSLContext
import javax.net.ssl.TrustManager
import javax.net.ssl.X509TrustManager
import kotlin.system.exitProcess

/**
 * Email:angcyo@126.com
 * @author angcyo
 * @date 2020/12/28
 */
open class BaseApp : Application() {

    companion object {
        lateinit var app: BaseApp

        fun getLogo(): Image? = getImageFx("logo.png")

        fun exit() {
            Platform.exit()
            exitProcess(0)
        }

        fun initSSL() {
            System.setProperty("sun.net.http.allowRestrictedHeaders", "true")
            val trm: TrustManager = object : X509TrustManager {
                override fun checkClientTrusted(certs: Array<X509Certificate?>?, authType: String?) {}
                override fun checkServerTrusted(certs: Array<X509Certificate?>?, authType: String?) {}
                override fun getAcceptedIssuers(): Array<X509Certificate>? = null
            }

            val sc: SSLContext = SSLContext.getInstance("SSL")
            sc.init(null, arrayOf(trm), null)
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.socketFactory)
        }
    }

    init {
        //JavaFX Application Thread
        mainScheduler = JavaFxScheduler.platform()
        L.init("JavaFx", L.debug)
    }

    /**主舞台*/
    lateinit var primaryStage: Stage


    /**1: JavaFX-Launcher*/
    override fun init() {
        super.init()

        //mac
        //bounds:Rectangle2D [minX = 0.0, minY=0.0, maxX=1920.0, maxY=1200.0, width=1920.0, height=1200.0]
        //visualBounds:Rectangle2D [minX = 0.0, minY=25.0, maxX=1920.0, maxY=1156.0, width=1920.0, height=1131.0]
        //dpi:147.0 outputScale:(2.0,2.0)

        //win
        //bounds:Rectangle2D [minX = 0.0, minY=0.0, maxX=1920.0, maxY=1080.0, width=1920.0, height=1080.0]
        //visualBounds:Rectangle2D [minX = 0.0, minY=0.0, maxX=1920.0, maxY=1040.0, width=1920.0, height=1040.0]
        //dpi:92.0 outputScale:(1.0,1.0)

        //val primary = Screen.getPrimary()
        L.w(Screen.getScreens())
        //Tray.addTray()

        println("Default Charset=" + Charset.defaultCharset())
        println("file.encoding=" + System.getProperty("file.encoding"))
        /*Default Charset=UTF-8
        file.encoding=UTF-8*/

        /*Platform.runLater {

        }*/
    }

    /**2: JavaFX Application Thread*/
    override fun start(primaryStage: Stage) {
        this.primaryStage = primaryStage
        app = this
        //Tray.getSafeScreenBounds(Point(1, 1))
    }

    /**3: JavaFX Application Thread*/
    override fun stop() {
        super.stop()
        //Platform.exit()
    }

    /**退出应用程序*/
    open fun exit() {
        BaseApp.exit()
    }
}